<template>
	<view class="index-type-box">
		<view class="title-box">
			<view class="left">
				<image src="/static/index/index-title-bg.png" class="img"></image>
				<text class="text">{{props.title}}</text>
			</view>
			<view class="right" @tap="handleTap">
				<text class="text">更多</text>
				<image src="/static/index/other.png" class="img"></image>
			</view>
		</view>
		<view class="list-box">
			<view class="itembox" v-for="(item,index) in dataList" :key="index" @tap="goToDetail(item.courseId)">
				<up-lazy-load :image="item.titleImg" imgMode="aspectFill" height="152" style="border-radius: 12rpx;"
					loading-img="../../static/imgload.png"></up-lazy-load>
				<text class="title">{{item.title.replace(/\[完\]|\[连\]/g, "")}}</text>
				<text class="author">{{item.author?.authorName}}</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		defineExpose,
		onMounted
	} from 'vue';
	// import {
	// 	onLoad
	// } from '@dcloudio/uni-app';
	import comicApi from '@/api/comicApi';
	import {
		debounce
	} from 'lodash';


	const props = defineProps({
		title: {
			type: String
		},
		classifyId: {
			type: String
		}
	})
	const dataList = ref([]);

	// 查询数据
	const getData = async () => {
		const result = await comicApi.list({
			page: 1,
			limit: 6,
			classifyId: props.classifyId
		});
		dataList.value = result.data?.list;
	};


	// 打开分类列表页
	const handleTap = debounce(() => {
		uni.navigateTo({
			url: `/pages/comic/typeList?title=${props.title}&classifyId=${props.classifyId}`
		})
	});

	// 打开详情页
	const goToDetail = debounce((courseId) => {
		uni.navigateTo({
			url: `/pages/comic/details?courseId=${courseId}`
		})
	}, 300);

	defineExpose({
		reload: getData
	})

	onMounted(() => {
		getData();
	})
</script>

<style lang="scss" scoped>
	.index-type-box {
		.title-box {
			display: flex;
			justify-content: space-between;
			border-bottom: 2rpx dashed #D9D9D9;
			height: 60rpx;

			.left {
				.text {
					color: #9E551D;
					font-size: 36rpx;
					font-style: normal;
					font-weight: 750;
					margin-left: -25rpx;
				}

				.img {
					width: 44rpx;
					height: 44rpx;
				}
			}

			.right {
				display: flex;
				justify-content: center;
				align-items: center;

				.text {
					color: #6C6C6C;
					font-size: 28rpx;
					font-style: normal;
					font-weight: 550;
					margin-right: 10rpx;
				}

				.img {
					width: 44rpx;
					height: 44rpx;
				}
			}
		}

		.list-box {
			margin-top: 20rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: flex-start;
			

			.itembox {
				width: 220rpx;
				display: flex;
				flex-direction: column;
				margin-right: 24rpx;
				margin-bottom: 40rpx;

				.title {
					color: #101010;
					font-size: 26rpx;
					text-align: center;
					margin-top: 12rpx;
					/* 必须使用弹性盒子布局 */
					display: -webkit-box;
					/* 限制显示的行数 */
					-webkit-line-clamp: 1;
					/* 垂直方向排列 */
					-webkit-box-orient: vertical;
					/* 超出部分隐藏 */
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.author {
					color: #999;
					text-align: center;
					font-size: 24rpx;
					font-weight: 550;
					margin-top: 8rpx;
				}
			}

			.itembox:nth-of-type(3n) {
				margin-right: 0;
			}
		}
	}
</style>